//
//  LoginViewController.m
//  ZFPlatform
//
//  Created by magus on 11-5-27.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RegisterViewController.h"
#import "CTextField.h"
#import "AgreementViewController.h"
#import "CardSettingViewController.h"


@implementation RegisterViewController

@synthesize phone;
@synthesize phoneError;
@synthesize email;
@synthesize	emailError;
@synthesize pwd;
@synthesize pwdError;
@synthesize rePwd;
@synthesize rePwdError;
@synthesize finishRegBtn;
@synthesize bgPanel;
@synthesize readAgreement;
@synthesize agreeBtn;


//通过xib资源初始化视图控制器
//
+ (RegisterViewController*) InitFromXib
{
	self = [[RegisterViewController alloc] 
			initWithNibName:@"RegisterViewController" bundle:nil];
	return self;
}


//初始化界面
//
- (void) InitAllViews
{
	//设置导航栏标题
	[self setTitle:@"注册"];
	
	//设置导航栏左按钮
	UIImage* image = [UIImage imageNamed: @"导航栏按钮（返回）.png"];
	CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
	UIButton* leftButton = [[UIButton alloc] initWithFrame:rect];
	[leftButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
	[leftButton setTitle:@" 返回" forState:UIControlStateNormal];
	[leftButton setBackgroundImage:image forState:UIControlStateNormal];
	[leftButton addTarget:self action:@selector(GoBack:) forControlEvents:UIControlEventTouchDown];
	UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
	self.navigationItem.leftBarButtonItem = leftItem;
	[leftItem release];
	[leftButton release];
	[image release];
	
	//设置背景面板成圆角显示
	[bgPanel.layer setCornerRadius:12.0];
	
	//设置“同意协议”图片
	UIImage* agreeImg = [UIImage imageNamed:@"未选中.png"];
	[agreeBtn setBackgroundImage:agreeImg forState:UIControlStateNormal];
	[agreeImg release];
	
	//设置“注册”按钮背景
	UIImage* registerImg = [UIImage imageNamed:@"按钮（大）.png"];
	[finishRegBtn setBackgroundImage:registerImg forState:UIControlStateNormal];
	[registerImg release];
}


//响应导航栏返回按钮
//
- (void) GoBack: (id) sender
{
	[self.navigationController popViewControllerAnimated:YES];
}


//使按return键后退出软键盘
//
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


//检查手机号码是否合法
//
- (BOOL) CheckPhone: (NSString*) strPhone
{
	int len = [strPhone length];
	
	if (len == 0)
	{
		phoneError.text = @"不能为空";
		return NO;
	}
	
    NSString *regex = @"[0-9]{11}"; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
	if ( ! [predicate evaluateWithObject:strPhone] )
	{
		phoneError.text = @"格式错误";
		return NO;
	}
	
    return YES;
}


//检查邮箱是否合法
//
- (BOOL) CheckEmail: (NSString*) strEmail
{
	int len = [strEmail length];
	
	if (len == 0)
	{
		emailError.text = @"不能为空";
		return NO;
	}
	
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
	if ( ! [predicate evaluateWithObject:strEmail] )
	{
		emailError.text = @"格式错误";
		return NO;
	}
	
    return YES;
}


//检查密码是否合法
//
- (BOOL) CheckPwd: (NSString*) strPwd
{
	int len = [strPwd length];
	
	if (len == 0)
	{
		pwdError.text = @"不能为空";
		return NO;
	}
	else if (len < 4)
	{
		pwdError.text = @"长度太短";
		return NO;
	}
	else if (len > 10)
	{
		pwdError.text = @"长度太长";
		return NO;
	}

    NSString* regex = @"[A-Z0-9a-z]{4,10}";
    NSPredicate* predicatet = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
	if( ! [predicatet evaluateWithObject:strPwd] )
	{
		pwdError.text = @"格式错误";
		return NO;
	}
	
    return YES;
}


//
//比较两次密码
- (BOOL) ComparePwd:(NSString*) strPwd withRePwd:(NSString*) strRePwd
{	
	if ([strRePwd compare:strPwd])
	{
		rePwdError.text = @"密码不匹配";
		return NO;
	}
	
	return YES;
}


//响应文本框开始编辑
//
- (IBAction) OnEditBegin: (id) sender
{
	CTextField* textField = (CTextField*)sender;
	
	[textField SetBorderWidth:4.0];
	[textField SetCornerRadius:8.0];
	[textField SetBorderColor:[UIColor orangeColor]];
	[textField setTextColor:[UIColor blackColor]];
	
	if (textField == phone)
	{
		phoneError.text = @"";
	}
	else if(textField == email)
	{
		emailError.text = @"";
	}
	else if(textField == pwd)
	{
		pwdError.text = @"";
	}
	else if(textField == rePwd)
	{
		rePwdError.text = @"";
	}
}


//响应文本框编辑完毕
//
- (IBAction) OnEditEnd: (id) sender
{
	CTextField* textField = (CTextField*)sender;
	
	[textField SetBorderWidth:0.0];
	[textField setTextColor:[UIColor lightGrayColor]];
	
	if (textField == phone)
	{
		NSString* strPhone = [phone text];
		[self CheckPhone:strPhone];
	}
	else if(textField == email)
	{
		NSString* strEmail = [email text];
		[self CheckEmail:strEmail];
	}
	else if(textField == pwd)
	{
		NSString* strPwd = [pwd text];
		[self CheckPwd:strPwd];
	}
	else if(textField == rePwd)
	{
		NSString* strPwd = [pwd text];
		NSString* strRePwd = [rePwd text];
		[self ComparePwd:strPwd withRePwd:strRePwd];
	}
}


//响应"同意协议"按钮
//
- (IBAction) OnClick_Agreement: (id) sender
{	
	if (!bAgree)
	{
		//设置“记住密码”图片
		UIImage* agreeImg = [UIImage imageNamed:@"选中.png"];
		[agreeBtn setBackgroundImage:agreeImg forState:UIControlStateNormal];
		[agreeImg release];
		bAgree = YES;
	}
	else
	{
		//设置“记住密码”图片
		UIImage* agreeImg2 = [UIImage imageNamed:@"未选中.png"];
		[agreeBtn setBackgroundImage:agreeImg2 forState:UIControlStateNormal];
		[agreeImg2 release];
		bAgree = NO;
	}
}


//响应"阅读协议"按钮
//
- (IBAction) OnClick_ReadAgreement: (id) sender
{
	if(agreementViewController == nil)
		agreementViewController = [AgreementViewController InitFromXib];
	
	[self.navigationController pushViewController:agreementViewController animated:YES];
}


//响应“完成注册”按钮
//
- (IBAction) OnClick_FinishReg: (id) sender
{
	NSString* strPhone = [phone text];
	NSString* strEmail = [email text];
	NSString* strPwd = [pwd text];
	NSString* strRePwd = [rePwd text];
	
	if (![self CheckPhone:strPhone] || ![self CheckEmail:strEmail] || 
		![self CheckPwd:strPwd] || ![self ComparePwd:strPwd withRePwd:strRePwd] ||
		!bAgree)
	{
		return;
	}
	
	
	//在此向服务器提交注册信息
	// ......
	
	
	//弹出提示信息
	UIAlertView* alert = [[UIAlertView alloc] 
						  initWithTitle:@"注册成功" 
						  message:@"您现在可以绑定信用卡了" 
						  delegate:nil cancelButtonTitle:@"确定"
						  otherButtonTitles:nil];
	[alert show];
	[alert release];
	
	//切换到“信用卡设置”
	if(cardSettingViewController == nil)
		cardSettingViewController = [CardSettingViewController InitFromXib];
	
	[self.navigationController pushViewController:cardSettingViewController animated:YES];
}


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//初始化界面
	[self InitAllViews];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc
{
	[cardSettingViewController release];
	[agreementViewController release];
	[agreeBtn release];
	[readAgreement release];
	[bgPanel release];
	[phone release];
	[phoneError release];
	[email release];
	[emailError release];
	[pwd release];
	[pwdError release];
	[rePwd release];
	[rePwdError release];
	[finishRegBtn release];
	 
    [super dealloc];
}


@end
